﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RadowCore.Models
{
    /// <summary>
    /// A node in the exception tree which represents a CNF among FlowNode objects.
    /// </summary>
    class ConjunctNode:TreeNode
    {
        private List<OpNode> m_flows;
        /// <summary>
        /// a list of flows which are separated by conjunctions in the exception part of the policy rule.
        /// </summary>
        internal List<OpNode> Flows
        {
            get { return m_flows; }
            set { m_flows = value; }
        }
        /// <summary>
        /// ConjunctNode class constructor
        /// </summary>
        /// <param name="flows">A list of FlowNode elements</param>
        public ConjunctNode(List<OpNode> flows)
        {
            this.m_flows = flows;
        }

        public ConjunctNode()
        {
            // TODO: Complete member initialization
        }
        /// <summary>
        /// evaluates the truth value of the CNF clause ie. the conjunction among elements of m_flows
        /// </summary>
        /// <returns>the truth value of the CNF clause</returns>
        public override bool Evaluate(List<PGNode> visitedNodes)
        {
            //if the flow list is empty then return false value
            if (this.m_flows.Count == 0)
                return false;

            //get the evaluation of the first flow child
            bool result = this.m_flows[0].Evaluate(visitedNodes);

            //iterate over all the flows in the list and calculate the conjunction
            for (int i = 1; i < this.m_flows.Count(); i++)
                result = result && this.m_flows[i].Evaluate(visitedNodes);
            
            return result;
        }
    }
}
